package com.gidgabs.modules.api.service.share;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gidgabs.config.GidgabsContants;
import com.gidgabs.modules.dao.*;
import com.gidgabs.modules.entity.*;
import com.gidgabs.utils.SessionDataUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by tby on 2018/7/7.
 */
@Service
public class PaypalService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private TbUserJournalDao tbUserJournalDao;

    @Autowired
    private TbOrderDao orderDao;
    @Autowired
    private TbUserIncomeDao tbUserIncomeDao;

    @Autowired
    private TbOrderSuperDao orderSuperDao;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private TbButlerOrderDao butlerOrderDao;


    private static final Logger LOGGER = LoggerFactory.getLogger(PaypalService.class);


    @Transactional
    public void doneBusiness(String paymentId) {
        String orderIdStr = redisTemplate.opsForValue().get(paymentId);

        if (orderIdStr != null) {
            //发表日志,支付金额
            if (orderIdStr.startsWith(GidgabsContants.PAYPAL_ORDER_PRE_JOURNAL)) {
                String orderId = orderIdStr.replace(GidgabsContants.PAYPAL_ORDER_PRE_JOURNAL, "");
                TbUserJournalEntity tbUserJournalEntity = new TbUserJournalEntity();
                tbUserJournalEntity.setId(Integer.parseInt(orderId));
                tbUserJournalEntity.setStatus(1);
                tbUserJournalDao.updateNotNullTbUserJournalById(tbUserJournalEntity);
            }
            //支付子订单
            else if (orderIdStr.startsWith(GidgabsContants.PAYPAL_ORDER_PRE)) {
                String orderCode = orderIdStr.replace(GidgabsContants.PAYPAL_ORDER_PRE, "");
                TbOrderEntity tbOrderEntity = orderDao.selectTbOrderByCode(orderCode);
                //已支付 改变状态为2 待接收
                tbOrderEntity.setOrderStatus(2);
                orderDao.update(tbOrderEntity);
                //添加商品订单收入明细
                //添加收入明细
                TbUserIncomeEntity tbUserIncomeEntity = new TbUserIncomeEntity();
                tbUserIncomeEntity.setRevenueType(TbUserIncomeEntity.REVENUE_TYPE_RUN_LGE);
                tbUserIncomeEntity.setCreateTime(new Date());
                tbUserIncomeEntity.setDepict("这是商品订单收入明细");
                tbUserIncomeEntity.setLinkId(tbOrderEntity.getId());
                //收入=总价
                tbUserIncomeEntity.setPrice(tbOrderEntity.getOrderPrice());
                tbUserIncomeEntity.setUserId(SessionDataUtil.getCurrentUserId());
                tbUserIncomeDao.save(tbUserIncomeEntity);
            }
            //支付父级订单
            else if (orderIdStr.startsWith(GidgabsContants.PAYPAL_ORDER_SUPER_PRE)) {
                String orderCode = orderIdStr.replace(GidgabsContants.PAYPAL_ORDER_SUPER_PRE, "");
                TbOrderSuperEntity tbOrderSuperEntity = orderSuperDao.selectTbOrderSuperByCode(orderCode);
                //根据父级订单查询子级订单列表 并修改支付状态
                TbOrderEntity tbOrderEntity = new TbOrderEntity();
                tbOrderEntity.setOrderSuperId(tbOrderSuperEntity.getId());
                List<TbOrderEntity> tbOrderEntityList = orderDao.selectTbOrderByObj(tbOrderEntity);
                //循环修改子订单状态 和添加商品订单收入明细
                for (int i = 0; i < tbOrderEntityList.size(); i++) {
                    TbOrderEntity tbOrder = tbOrderEntityList.get(i);
                    //已支付 改变状态为2 待接收
                    tbOrder.setOrderStatus(2);
                    orderDao.update(tbOrder);
                    //添加商品订单收入明细
                    //添加收入明细
                    TbUserIncomeEntity tbUserIncomeEntity = new TbUserIncomeEntity();
                    tbUserIncomeEntity.setRevenueType(TbUserIncomeEntity.REVENUE_TYPE_RUN_LGE);
                    tbUserIncomeEntity.setCreateTime(new Date());
                    tbUserIncomeEntity.setDepict("这是商品订单收入明细");
                    tbUserIncomeEntity.setLinkId(tbOrder.getId());
                    //收入=总价
                    tbUserIncomeEntity.setPrice(tbOrder.getOrderPrice());
                    tbUserIncomeEntity.setUserId(SessionDataUtil.getCurrentUserId());
                    tbUserIncomeDao.save(tbUserIncomeEntity);

                }
            } else if (orderIdStr.startsWith(GidgabsContants.PAYPAL_ORDER_PRE_JOURNAL_REWARD)) {
                String journalRewardStr = redisTemplate.opsForValue().get(orderIdStr);
                try {
                    TbUserIncomeEntity tbUserIncomeEntity = objectMapper.readValue(journalRewardStr, TbUserIncomeEntity.class);
                    tbUserIncomeDao.save(tbUserIncomeEntity);
                } catch (IOException e) {
                    LOGGER.error("日志打赏回调处理错误", e);
                }
            }
            //支付跑腿订单
            else if (orderIdStr.startsWith(GidgabsContants.PAYPAL_BUTLER_ORDER__PRE)) {
                String orderCode = orderIdStr.replace(GidgabsContants.PAYPAL_BUTLER_ORDER__PRE, "");
                //支付完成 修改状态为待接单
                TbButlerOrderEntity tbButlerOrderEntity = butlerOrderDao.selectTbOrderByCode(orderCode);
                //待接单
                tbButlerOrderEntity.setOrderStatus(1);
                tbButlerOrderEntity.setUpdateTime(new Date());
                butlerOrderDao.update(tbButlerOrderEntity);
            }
        }
    }
}
